package com.da.javalog.validator;

import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

/**
 * @author chenlida
 * @date 2020/9/13 22:27
 * @description
 */
@Service
public class ValidatorWrapper implements ApplicationContextAware {
    private ApplicationContext applicationContext;

    /**
     * 条件校验
     */
    public String doValidate(ValidatorContext context) {
        Map<String, Validator> validators = this.applicationContext
            .getBeansOfType(Validator.class);

        // 执行校验
        for (Validator validator : validators.values()) {
            validator.doValidator(context);
        }

        // 返回校验结果
        return context.getMsg();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
